// 导入自定义的 HTTP 工具库

import http from '@/common/http.js'; 
import * as backendApi from '@/api/backend';
import { addPet, queryUserPets, deletePet, } from '@/api/backend';

// import http from '@/common/http.js';
// import { addPet, queryUserPets, deletePet } from '@/api/backend';

const STORAGE_KEY_PET_LIST = "pet_list";
let event = [{
  isComplete: true,
  id: 11,
  title: '运动',
  notes: '运动好身体好',
}, {
  isComplete: true,
  id: 12,
  title: '喂食',
  notes: '是时候吃饭啦',
}];
let event1 = [{
  isComplete: true,
  id: 11,
  title: '游泳',
  notes: '运动好身体好',
}, {
  isComplete: true,
  id: 10,
  title: '溜圈',
  notes: '悠闲的下午时间',
}];
let event2 = [{
  isComplete: true,
  id: 11,
  title: '游泳',
  notes: '运动好身体好',
}];
export default {
  namespaced: true,  // 启用命名空间，允许模块化管理状态
  state: {
    petList: [], // 用于存储宠物列表的状态
  },
  getters: {
    // 计算新的宠物 ID，通常用于添加新宠物
    newId(state) {
      if (state.petList.length > 0) {
        const lastIndex = state.petList.length - 1;
        const lastPetId = state.petList[lastIndex].id;
        return lastPetId ? (Number.parseInt(lastPetId) + 1).toString() : "1";
      }
      return "1";
    },
    // 获取宠物信息 id
    getPetById: (state) => (id) => {
      return state.petList.find(pet => pet.id === Number(id));
    },
    getModuleIndexState: (state, getters, rootState) => {
      // console.log(rootState.userInfo.openId, 'sssssssss')
      return rootState.userInfo.openId;
    },
  },
  mutations: {
    // 设置宠物列表
    setPetList(state, petList) {
      state.petList = petList;
      console.log(state.petList);
    },
    // 添加新宠物
    addPet(state, pet) {
      state.petList.push(pet);
    },
    // 根据 ID 删除宠物
    deletePet(state, id) {
      state.petList = state.petList.filter(pet => pet.id !== id);
    },
    // 编辑宠物信息
    // editPet(state, newPet) {
    //   const index = state.petList.findIndex(pet => pet.id === newPet.id);
    //   if (index !== -1) {
    //     Vue.set(state.petList, index, newPet); // 使用 Vue.set 以保证响应性
    //   }
    // },
    // 添加宠物计划
    addPetPlan(state, { petId, plan }) {
      const pet = state.petList.find(pet => pet.id === petId);
      if (pet) {
        pet.plans.push(plan);
      }
    },
    // 删除宠物计划
    deletePetPlan(state, { petId, planId }) {
      const pet = state.petList.find(pet => pet.id === petId);
      if (pet) {
        pet.plans = pet.plans.filter(plan => plan.id !== planId);
      }
    },
    // 完成宠物计划
    completePetPlan(state, { petId, planId }) {
      const pet = state.petList.find(pet => pet.id === petId);
      const plan = pet.plans.find(plan => plan.id === planId);
      if (plan) {
        plan.isComplete = true;
      }
    },
  },
  actions: {
    // 从服务器获取宠物列表并初始化状态
    async initPetList({ commit, getters }) {
      const openId = getters.getModuleIndexState
      await queryUserPets({ openId }).then(res => {
        let r = res.data.forEach((e, index) => {
          if (index === 1) e.plan = event1
          else if (index === 2) e.plan = event2
          else e.plan = event
          // event[0].title += 1

        });

        commit('setPetList', res.data);
      })
    },
    // 根据 ID 获取宠物信息，可以从状态或后端获取
    getPetById({ state }, id) {
      return state.petList.find(pet => pet.id === id) || http.getPetById({ id }).then(response => response.data);
    },
    // 调用 API 删除宠物并更新状态
    deletePetById({ commit }, id) {
      // 这个看你的接口是什么样的应该至少要传openId和你要删除的那个字段的id
      // 需要做的就是拿到这个要删除字段的id 
      // const openId = getters.getModuleIndexState
      const param = {
        // openId,
        id
      }
      deletePet(param).then((res) => {
        // res是返回的值
        commit('deletePet', id);
      });
    },
    // 为新宠物分配 ID，并添加到状态和服务器
    addPetToLast({ commit, getters }, pet) {
      const openId = getters.getModuleIndexState
      // 这里目前是固定的状态，后面记得改，那个页面改好，这里直接用下面这个就可以
      const param = {
        openId,
        ...pet
      }

      // const param = {
      //  openId,
      //  ageYear: 30,
      //  ageMonth: 76,
      //  breed: "1111",
      //  weight: 75,
      //  name: "专些共何6",
      //  photoUrl: "http://dummyimage.com/400x400"
      // }
      addPet(param).then(res => {
        commit('addPet', pet);
      })
    },
    // 编辑宠物信息并更新状态
    editPet({ commit,getters }, pet) {
		const openId = getters.getModuleIndexState
		// 这里目前是固定的状态，后面记得改，那个页面改好，这里直接用下面这个就可以
		const param = {
				  ...pet
		}
		backendApi.editPetById(param).then(res => {
				 console.log(res.data)
				  // commit('addPet', pet);
		})
    },
    // 添加宠物计划到状态和服务器
    addPetPlanById({ commit }, { petId, plan }) {
      http.addFeedingLog({ petId, plan }).then(() => {
        commit('addPetPlan', { petId, plan });
      });
    },
    // 删除宠物计划并更新状态
    deletePetPlanById({ commit }, { petId, planId }) {
      http.deleteFeedingLog({ petId, planId }).then(() => {
        commit('deletePetPlan', { petId, planId });
      });
    },
    // 完成宠物计划，目前没有具体 API 调用
    completePetPlanById({ commit }, { petId, planId }) {
      commit('completePetPlan', { petId, planId });
    },
  },
};
